home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / test / test15.c < prev    next >
C/C++ Source or Header  |  1990-07-20  |  20KB  |  592 lines

  1. /* Test program for string(3) routines.
  2.  *
  3.  * Note that at least one Bell Labs implementation of the string
  4.  * routines flunks a couple of these tests -- the ones which test
  5.  * behavior on "negative" characters.
  6.  */
  7.  
  8. #include <sys/types.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12.  
  13. #define    STREQ(a, b)    (strcmp((a), (b)) == 0)
  14.  
  15. char *it = "<UNSET>";        /* Routine name for message routines. */
  16. int waserror = 0;        /* For exit status. */
  17.  
  18. char uctest[] = "\004\203";    /* For testing signedness of chars. */
  19. int charsigned;            /* Result. */
  20.  
  21. /*
  22.  - check - complain if condition is not true
  23.  */
  24. void check(thing, number)
  25. int thing;
  26. int number;            /* Test number for error message. */
  27. {
  28.   if (!thing) {
  29.     printf("\t%s flunked test %d\n", it, number);
  30.     waserror = 1;
  31.   }
  32. }
  33.  
  34. /*
  35.  - equal - complain if first two args don't strcmp as equal
  36.  */
  37. void equal(a, b, number)
  38. char *a;
  39. char *b;
  40. int number;            /* Test number for error message. */
  41. {
  42.   check(a != NULL && b != NULL && STREQ(a, b), number);
  43. }
  44.  
  45. char one[50];
  46. char two[50];
  47.  
  48. #ifdef UNIXERR
  49. #define ERR 1
  50. #endif
  51. #ifdef BERKERR
  52. #define ERR 1
  53. #endif
  54. #ifdef ERR
  55. int f;
  56. extern char *sys_errlist[];
  57. extern int sys_nerr;
  58. extern int errno;
  59. #endif
  60.  
  61. /* ARGSUSED */
  62. main(argc, argv)
  63. int argc;
  64. char *argv[];
  65. {
  66.  
  67.   printf("Test 15 ");
  68.  
  69.   /* First, establish whether chars are signed. */
  70.   if (uctest[0] < uctest[1])
  71.     charsigned = 0;
  72.   else
  73.     charsigned = 1;
  74.  
  75.   /* Then, do the rest of the work.  Split into two functions because
  76.    * some compilers get unhappy about a single immense function. */
  77.   first();
  78.   second();
  79.  
  80.   printf("ok\n");
  81.   exit((waserror) ? 1 : 0);
  82. }
  83.  
  84. first()
  85. {
  86.   /* Test strcmp first because we use it to test other things. */
  87.   it = "strcmp";
  88.   check(strcmp("", "") == 0, 1);/* Trivial case. */
  89.   check(strcmp("a", "a") == 0, 2);    /* Identity. */
  90.   check(strcmp("abc", "abc") == 0, 3);    /* Multicharacter. */
  91.   check(strcmp("abc", "abcd") < 0, 4);    /* Length mismatches. */
  92.   check(strcmp("abcd", "abc") > 0, 5);
  93.   check(strcmp("abcd", "abce") < 0, 6);    /* Honest miscompares. */
  94.   check(strcmp("abce", "abcd") > 0, 7);
  95.   check(strcmp("a\203", "a") > 0, 8);    /* Tricky if char signed. */
  96.  
  97. /* This gives problems DEBUG
  98.   if (charsigned)    
  99.     check(strcmp("a\203", "a\003") < 0, 9);
  100.   else
  101.     check(strcmp("a\203", "a\003") > 0, 9);
  102. */
  103.  
  104.   /* Test strcpy next because we need it to set up other tests. */
  105.   it = "strcpy";
  106.   check(strcpy(one, "abcd") == one, 1);    /* Returned value. */
  107.   equal(one, "abcd", 2);    /* Basic test. */
  108.  
  109.   (void) strcpy(one, "x");
  110.   equal(one, "x", 3);        /* Writeover. */
  111.   equal(one + 2, "cd", 4);    /* Wrote too much? */
  112.  
  113.   (void) strcpy(two, "hi there");
  114.   (void) strcpy(one, two);
  115.   equal(one, "hi there", 5);    /* Basic test encore. */
  116.   equal(two, "hi there", 6);    /* Stomped on source? */
  117.  
  118.   (void) strcpy(one, "");
  119.   equal(one, "", 7);        /* Boundary condition. */
  120.  
  121.   /* Strcat */
  122.   it = "strcat";
  123.   (void) strcpy(one, "ijk");
  124.   check(strcat(one, "lmn") == one, 1);    /* Returned value. */
  125.   equal(one, "ijklmn", 2);    /* Basic test. */
  126.  
  127.   (void) strcpy(one, "x");
  128.   (void) strcat(one, "yz");
  129.   equal(one, "xyz", 3);        /* Writeover. */
  130.   equal(one + 4, "mn", 4);    /* Wrote too much? */
  131.  
  132.   (void) strcpy(one, "gh");
  133.   (void) strcpy(two, "ef");
  134.   (void) strcat(one, two);
  135.   equal(one, "ghef", 5);    /* Basic test encore. */
  136.   equal(two, "ef", 6);        /* Stomped on source? */
  137.  
  138.   (void) strcpy(one, "");
  139.   (void) strcat(one, "");
  140.   equal(one, "", 7);        /* Boundary conditions. */
  141.   (void) strcpy(one, "ab");
  142.   (void) strcat(one, "");
  143.   equal(one, "ab", 8);
  144.   (void) strcpy(one, "");
  145.   (void) strcat(one, "cd");
  146.   equal(one, "cd", 9);
  147.  
  148.   /* Strncat - first test it as strcat, with big counts, then test the
  149.    * count mechanism. */
  150.   it = "strncat";
  151.   (void) strcpy(one, "ijk");
  152.   check(strncat(one, "lmn", 99) == one, 1);    /* Returned value. */
  153.   equal(one, "ijklmn", 2);    /* Basic test. */
  154.  
  155.   (void) strcpy(one, "x");
  156.   (void) strncat(one, "yz", 99);
  157.   equal(one, "xyz", 3);        /* Writeover. */
  158.   equal(one + 4, "mn", 4);    /* Wrote too much? */
  159.  
  160.   (void) strcpy(one, "gh");
  161.   (void) strcpy(two, "ef");
  162.   (void) strncat(one, two, 99);
  163.   equal(one, "ghef", 5);    /* Basic test encore. */
  164.   equal(two, "ef", 6);        /* Stomped on source? */
  165.  
  166.   (void) strcpy(one, "");
  167.   (void) strncat(one, "", 99);
  168.   equal(one, "", 7);        /* Boundary conditions. */
  169.   (void) strcpy(one, "ab");
  170.   (void) strncat(one, "", 99);
  171.   equal(one, "ab", 8);
  172.   (void) strcpy(one, "");
  173.   (void) strncat(one, "cd", 99);
  174.   equal(one, "cd", 9);
  175.  
  176.   (void) strcpy(one, "ab");
  177.   (void) strncat(one, "cdef", 2);
  178.   equal(one, "abcd", 10);    /* Count-limited. */
  179.  
  180.   (void) strncat(one, "gh", 0);
  181.   equal(one, "abcd", 11);    /* Zero count. */
  182.  
  183.   (void) strncat(one, "gh", 2);
  184.   equal(one, "abcdgh", 12);    /* Count and length equal. */
  185.  
  186.   /* Strncmp - first test as strcmp with big counts, then test count code. */
  187.   it = "strncmp";
  188.   check(strncmp("", "", 99) == 0, 1);    /* Trivial case. */
  189.   check(strncmp("a", "a", 99) == 0, 2);    /* Identity. */
  190.   check(strncmp("abc", "abc", 99) == 0, 3);    /* Multicharacter. */
  191.   check(strncmp("abc", "abcd", 99) < 0, 4);    /* Length unequal. */
  192.   check(strncmp("abcd", "abc", 99) > 0, 5);
  193.   check(strncmp("abcd", "abce", 99) < 0, 6);    /* Honestly unequal. */
  194.   check(strncmp("abce", "abcd", 99) > 0, 7);
  195.   check(strncmp("a\203", "a", 2) > 0, 8);    /* Tricky if '\203' < 0 */
  196.  
  197. /*  This gives problems -- DEBUG
  198.   if (charsigned)
  199.     check(strncmp("a\203", "a\003", 2) < 0, 9);
  200.   else
  201.     check(strncmp("a\203", "a\003", 2) > 0, 9);
  202. */
  203.  
  204.   check(strncmp("abce", "abcd", 3) == 0, 10);    /* Count limited. */
  205.   check(strncmp("abce", "abc", 3) == 0, 11);    /* Count == length. */
  206.   check(strncmp("abcd", "abce", 4) < 0, 12);    /* Nudging limit. */
  207.   check(strncmp("abc", "def", 0) == 0, 13);    /* Zero count. */
  208.  
  209.   /* Strncpy - testing is a bit different because of odd semantics */
  210.   it = "strncpy";
  211.   check(strncpy(one, "abc", 4) == one, 1);    /* Returned value. */
  212.   equal(one, "abc", 2);        /* Did the copy go right? */
  213.  
  214.   (void) strcpy(one, "abcdefgh");
  215.   (void) strncpy(one, "xyz", 2);
  216.   equal(one, "xycdefgh", 3);    /* Copy cut by count. */
  217.  
  218.   (void) strcpy(one, "abcdefgh");
  219.   (void) strncpy(one, "xyz", 3);/* Copy cut just before NUL. */
  220.   equal(one, "xyzdefgh", 4);
  221.  
  222.   (void) strcpy(one, "abcdefgh");
  223.   (void) strncpy(one, "xyz", 4);/* Copy just includes NUL. */
  224.   equal(one, "xyz", 5);
  225.   equal(one + 4, "efgh", 6);    /* Wrote too much? */
  226.  
  227.   (void) strcpy(one, "abcdefgh");
  228.   (void) strncpy(one, "xyz", 5);/* Copy includes padding. */
  229.   equal(one, "xyz", 7);
  230.   equal(one + 4, "", 8);
  231.   equal(one + 5, "fgh", 9);
  232.  
  233.   (void) strcpy(one, "abc");
  234.   (void) strncpy(one, "xyz", 0);/* Zero-length copy. */
  235.   equal(one, "abc", 10);
  236.  
  237.   (void) strncpy(one, "", 2);    /* Zero-length source. */
  238.   equal(one, "", 11);
  239.   equal(one + 1, "", 12);
  240.   equal(one + 2, "c", 13);
  241.  
  242.   (void) strcpy(one, "hi there");
  243.   (void) strncpy(two, one, 9);
  244.   equal(two, "hi there", 14);    /* Just paranoia. */
  245.   equal(one, "hi there", 15);    /* Stomped on source? */
  246.  
  247.   /* Strlen */
  248.   it = "strlen";
  249.   check(strlen("") == 0, 1);    /* Empty. */
  250.   check(strlen("a") == 1, 2);    /* Single char. */
  251.   check(strlen("abcd") == 4, 3);/* Multiple chars. */
  252.  
  253.   /* Strchr */
  254.   it = "strchr";
  255.   check(strchr("abcd", 'z') == NULL, 1);    /* Not found. */
  256.   (void) strcpy(one, "abcd");
  257.   check(strchr(one, 'c') == one + 2, 2);    /* Basic test. */
  258.   check(strchr(one, 'd') == one + 3, 3);    /* End of string. */
  259.   check(strchr(one, 'a') == one, 4);    /* Beginning. */
  260.   check(strchr(one, '\0') == one + 4, 5);    /* Finding NUL. */
  261.   (void) strcpy(one, "ababa");
  262.   check(strchr(one, 'b') == one + 1, 6);    /* Finding first. */
  263.   (void) strcpy(one, "");
  264.   check(strchr(one, 'b') == NULL, 7);    /* Empty string. */
  265.   check(strchr(one, '\0') == one, 8);    /* NUL in empty string. */
  266.  
  267.   /* Index - just like strchr */
  268.   it = "index";
  269.   check(index("abcd", 'z') == NULL, 1);    /* Not found. */
  270.   (void) strcpy(one, "abcd");
  271.   check(index(one, 'c') == one + 2, 2);    /* Basic test. */
  272.   check(index(one, 'd') == one + 3, 3);    /* End of string. */
  273.   check(index(one, 'a') == one, 4);    /* Beginning. */
  274.   check(index(one, '\0') == one + 4, 5);    /* Finding NUL. */
  275.   (void) strcpy(one, "ababa");
  276.   check(index(one, 'b') == one + 1, 6);    /* Finding first. */
  277.   (void) strcpy(o